package edu.csula.cs575;




import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class WifiList extends Activity implements OnClickListener{
	
	private static final String TAG = "WiFiDemo";
	WifiManager wifi;
	BroadcastReceiver receiver;
	ArrayList<WiFiView> arrayList;
	ListView list;
	Button buttonScan;
	ListAdapter sca2;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wifilist);
		
		// Setup UI
		list = (ListView)this.findViewById(R.id.wifilistView);
		list.setItemsCanFocus(false);
		list.setChoiceMode(ListView.PERSISTENT_ANIMATION_CACHE);
		buttonScan = (Button) findViewById(R.id.buttonScan);
		buttonScan.setOnClickListener(this);
		arrayList =  new ArrayList<WiFiView>();
		// Setup WiFi
		wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		if (wifi.isWifiEnabled() == false)
        {
            Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(true);
        } 
		// Get WiFi status
		//WifiInfo info = wifi.getConnectionInfo();
		//textStatus.append("\n\nWiFi Status: " + info.toString());

		// List available networks
		List<ScanResult> result = wifi.getScanResults();
		WiFiView item_details;
		if (result != null){
		for (ScanResult config : result) {
			item_details = new WiFiView();
			item_details.setName(config.SSID);
			
			item_details.setCapabilities(config.capabilities.replace("[", " ").replaceAll("]", " ").replaceAll("-", " "));
			config.level =config.level+100;
			if(config.level>0 && config.level<20)item_details.setImageNumber(1);
			else if (config.level>20 && config.level<40)item_details.setImageNumber(2);
			else if (config.level>40 && config.level<80)item_details.setImageNumber(3);
			else item_details.setImageNumber(4);;
			item_details.setLevel(String.valueOf(config.level)+"%");
			arrayList.add(item_details);
			
		}
		}
		list.setAdapter(new WiFiViewAdaptor(this, arrayList));
		// Register Broadcast Receiver
		}

	

	public void onClick(View view) {

		if (view.getId() == R.id.buttonScan) {
			arrayList.clear();
			wifi.startScan();
			List<ScanResult> result = wifi.getScanResults();
			WiFiView item_details;
			if (result != null){
			for (ScanResult config : result) {
				item_details = new WiFiView();
				item_details.setName(config.SSID);
				item_details.setCapabilities(config.capabilities.replace("[", " ").replaceAll("]", " ").replaceAll("-", " "));
				config.level =config.level+100;
				if(config.level>0 && config.level<20)item_details.setImageNumber(1);
				else if (config.level>20 && config.level<40)item_details.setImageNumber(2);
				else if (config.level>40 && config.level<80)item_details.setImageNumber(4);
				else item_details.setImageNumber(4);;
				item_details.setLevel(String.valueOf(config.level)+"%");
				arrayList.add(item_details);
				
			}
			list.setAdapter(new WiFiViewAdaptor(this, arrayList));
		}
		}
	}
	
}